package com.cg.bank.factory;

import com.cg.bank.dao.BankDao;

import java.io.IOException;
import java.util.Properties;

/**
 * @author chunges
 * @version V1.0
 * @Description BankDao工厂
 * @date 2018年7月17日 上午11:07:52
 */

public class BankDaoFactory extends AbstractFactory<BankDao> {
    // 工厂配置文件路径
    // private static final String FACTORYPATH = "config/factory/dao.properties";
    private static final String CLASSNAME = "className";

    private BankDaoFactory() {
        //NOOP
    }

    @Override
    protected BankDao createProduct(String className)
            throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        return (BankDao) Class.forName(className).newInstance();
    }

    /**
     * 获得DaoImpl类名
     *
     * @return
     * @throws IOException
     */
    private String getClassName() throws IOException {
        Properties properties = new Properties();
        properties.load(this.getClass().getClassLoader().getResourceAsStream("../config/factory/dao.properties"));
        return properties.getProperty(CLASSNAME);
    }

    /**
     * 获得BankDao
     *
     * @return
     */
    public BankDao getBankDaoInstance() throws IOException, ReflectiveOperationException {
        return createProduct(getClassName());
    }

    /**
     * BankDaoFactory生成器
     *
     * @author chunges
     */
    private static class BankDaoFactoryHolder {
        private static final BankDaoFactory instance = new BankDaoFactory();
    }

    /**
     * 获得BankDaoFactory实例
     *
     * @return BankDaoFactory instance BankDaoFactory实例
     */
    public static BankDaoFactory getInstance() {
        return BankDaoFactoryHolder.instance;
    }
}